//
//  BRInviteCodeView.m
//  BrezzeSaaS
//
//  Created by IOS on 2022/1/19.
//  Copyright © 2022 Brezze GWG. All rights reserved.
//

#import "BRInviteCodeView.h"
#import "BRInviteRewardView.h"
@interface BRInviteCodeView ()<QMUITextFieldDelegate>
{
    QMUIButton *_submitBtn;
    QMUITextField *_inputTF;
    
    QMUILabel *_codeL;
}
@end

@implementation BRInviteCodeView


- (instancetype)initCodeView
{
    self = [super init];
    if (self) {
        
        [self setUI:0];
    }
    return self;
}

- (instancetype)initCodeInviteView
{
    self = [super init];
    if (self) {
        
        [self setUI:1];
    }
    return self;
}

- (void)toAbout {
    
    if (self.toAboutBlock) self.toAboutBlock();
}

- (void)setCodeStr:(NSString *)codeStr {
   
    if (_codeL) {
        _codeL.text  = codeStr;
    }
}

- (NSString *)codeStr {
    NSString *invStr = [BRUserManager share].userModel.inviteCode;
    if (invStr.length != 0) {
        return invStr;
    }
    return @"--";
}

- (NSString *)inputStr {
    return _inputTF.text;
}

- (void)setInputStr:(NSString *)inputStr {
    if (_inputTF) {
        _inputTF.text = inputStr;
        _submitBtn.enabled = YES;
    }
}

/// 跳转邀请奖励3 和 历史2
- (void)btnAction:(QMUIButton *)btn {
    NSInteger index = btn.tag - 100;
    if (self.inviteClickBlock) {
        self.inviteClickBlock(index,@"");
    }
}

- (void)submitAction:(QMUIButton *)btn {
    
   
    NSString *str = self.codeStr;

    if (self.inviteClickBlock) {
        self.inviteClickBlock(1,str);
    }
    
}

- (void)copyAction {
    if (_codeL) {
        UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
        pasteboard.string = self.codeStr;
        [BRHud showAuto:Localized(@"invite_copied")];
    }
}

#pragma mark - textField delegate --
- (void)textFieldDidChangeSelection:(UITextField *)textField {
    
    _submitBtn.enabled = [BRTools checkIsInviteCode:textField.text];
    
    if (textField.text.length >= 8) {
//        [textField resignFirstResponder];
    }
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    
    if ([string isEqualToString:@""]) {
        return YES;
    }
    return [BRTools checkIsNumberABC:string];
//    return [BRTools canInputText:string];
}

- (void)showShadow {
    self.layer.shadowColor = [UIColor colorWithRed:0/255.0 green:0/255.0 blue:0/255.0 alpha:0.06].CGColor;
    self.layer.shadowOffset = CGSizeMake(0,10);
    self.layer.shadowRadius = 16;
    self.layer.shadowOpacity = 1;
    self.layer.masksToBounds = NO;
}

#pragma mark - UI --
- (void)setUI:(NSInteger)type {
    
    self.backgroundColor = UIColor.whiteColor;
    self.layer.cornerRadius = 10;
    self.layer.masksToBounds = YES;
    
    QMUILabel *titleL = [BRToolsView br_label:Localized(@"invite_inviteRewards") textColor:kColorTitle font:[UIFont fontB:17] textAlignment:NSTextAlignmentCenter];
    titleL.numberOfLines = 0;
    [self addSubview:titleL];
    [titleL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(28);
        make.left.offset(28);
        make.right.offset(-28);
    }];
    
    UIStackView *stackView = [[UIStackView alloc] init];
    stackView.alignment = UIStackViewAlignmentFill;
    stackView.distribution = UIStackViewDistributionFillEqually;
    stackView.spacing = 0;
    [self addSubview:stackView];//104(85) + 64
    [stackView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(titleL.mas_bottom).offset(4);
        make.left.offset(20);
        make.right.offset(-20);
//        make.height.mas_equalTo(134);
    }];
    NSArray *titles = @[Localized(@"points_points"),Localized(@"menu_coupon")];
    NSArray *imgs = @[@"invite_points",@"invite_coupon"];
    for (int i = 0; i < titles.count; i++) {
        
        BRInviteTopImgView *itemV = [[BRInviteTopImgView alloc] initWithImg:imgs[i] title:titles[i]];
        [stackView addArrangedSubview:itemV];
        itemV.userInteractionEnabled = YES;
        itemV.tag = 100 + i;
    }
    
    QMUIButton *inviteBtn = [BRToolsView br_btnBGImg:Localized(@"invite_shareInvite")];
    _submitBtn = inviteBtn;
    [inviteBtn setImage:UIImageMake(@"invite_link") forState:UIControlStateNormal];
    inviteBtn.imagePosition = QMUIButtonImagePositionRight;
    inviteBtn.imageEdgeInsets = UIEdgeInsetsMake(0, 8, 0, 0);
    [self addSubview:inviteBtn];
    [inviteBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(stackView.mas_bottom).offset(14);
        make.height.mas_equalTo(56);
        make.left.equalTo(self).offset(20);
        make.right.equalTo(self).offset(-20);
       
    }];
    [inviteBtn addTarget:self action:@selector(submitAction:) forControlEvents:UIControlEventTouchUpInside];
    
    QMUILabel *codeTitleL = [BRToolsView br_label:Localized(@"invite_shareCodeText") textColor:kColorTitle font:[UIFont fontS:14] textAlignment:NSTextAlignmentCenter];
    codeTitleL.numberOfLines = 0;
    [self addSubview:codeTitleL];
    [codeTitleL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(inviteBtn.mas_bottom).offset(20);
        make.left.offset(28);
        make.right.offset(-28);
        
    }];
    
    //-----// 邀请码文本
    UIView *codeView = [[UIView alloc] init];
    codeView.layer.cornerRadius = 26;
    codeView.layer.masksToBounds = YES;
    codeView.layer.borderColor = kColorMain.CGColor;
    codeView.layer.borderWidth = 1;
    codeView.backgroundColor = UIColor.whiteColor;
    [self addSubview:codeView];
    [codeView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(20);
        make.right.offset(-20);
        make.height.mas_equalTo(52);
        make.top.equalTo(codeTitleL.mas_bottom).offset(10);
        make.bottom.offset(-64);
    }];
    
    
    QMUILabel *codeL = [BRToolsView br_label:self.codeStr textColor:kColorMain font:[UIFont fontB:17] textAlignment:NSTextAlignmentRight];
    
    [codeView addSubview:codeL];
    [codeL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(codeView).offset(-8);
        make.centerY.equalTo(codeView);
    }];
    _codeL = codeL;
    
    UIImageView *copyImg = [[UIImageView alloc] init];
    copyImg.image = UIImageMake(@"invite_copy");
    [codeView addSubview:copyImg];
    [copyImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(13);
        make.height.mas_equalTo(16);
        make.centerY.equalTo(codeView);
        make.left.equalTo(codeL.mas_right).offset(6);
    }];
    
    codeView.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap  = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(copyAction)];
    [codeView addGestureRecognizer:tap];
    
    
    QMUIButton *abBtn = [[QMUIButton alloc] init];
    [abBtn setTitle:Localized(@"invite_aboutReferral") forState:UIControlStateNormal];
    [abBtn setImage:UIImageMake(@"arrow_right_green6") forState:UIControlStateNormal];
    abBtn.titleLabel.font = [UIFont fontM:14];
    [abBtn setTitleColor:kColorMain forState:UIControlStateNormal];
    abBtn.imagePosition = QMUIButtonImagePositionRight;
    abBtn.imageEdgeInsets = UIEdgeInsetsMake(0, 6, 0, 0);
    
    [self addSubview:abBtn];
    [abBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(44);
        make.centerX.offset(0);
        make.bottom.offset(-14);
    }];
    
    [abBtn addTarget:self action:@selector(toAbout) forControlEvents:UIControlEventTouchUpInside];

    
    
    return;
    
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end

@interface BRInviteModuleView ()
{
    BRInviteCodeView *_inviteView;
}

@end


@implementation BRInviteModuleView

+ (instancetype)shareInvite {
    
    static BRInviteModuleView *view = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        view = [[BRInviteModuleView alloc] init];
    });
    
    return view;
}


- (instancetype)init
{
    self = [super init];
    if (self) {
        [self setUI];
    }
    return self;
}

- (void)show {
    self.hidden = NO;
}

- (void)hidd {
    self.hidden = YES;
}

- (void)setCodeStr:(NSString *)codeStr {
    _codeStr = codeStr;
    _inviteView.inputStr = codeStr;
}

- (void)setUI {
    self.backgroundColor = UIColorClear;
    self.hidden = YES;
    
    UIView *shadow = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
    [self addSubview:shadow];
    shadow.userInteractionEnabled = YES;
    shadow.backgroundColor = UIColorShadow;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hidd)];
    [shadow addGestureRecognizer:tap];
    
    BRInviteCodeView *inviteView = [[BRInviteCodeView alloc] initCodeView];
    
    [self addSubview:inviteView];
    [inviteView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self);
        make.centerY.equalTo(self);
    }];
    self.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight);
    [DefaultTipsParentView addSubview:self];
    _inviteView = inviteView;
}

@end
